<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Skit;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Skit\Ctl\SkitCategoryCtl;
use Srv\Apps\Proto\DTO\Skit\SkitCategoryAddDTO;
use Srv\Apps\Proto\DTO\Skit\SkitCategoryEditDTO;
use Srv\Apps\Proto\DTO\Skit\SkitCategoryListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Skit\SkitCategoryListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class SkitCategoryAct extends ActionAbstract
{
    /**
     * @url /skit/category/list
     * @auth skitCategoryList
     * @req Srv.Apps.Proto.DTO.Skit.SkitCategoryListDTO
     * @rsp Srv.Apps.Proto.VO.Skit.SkitCategoryListVO
     * 短剧类别列表
     */
    public function getSkitCategoryList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof SkitCategoryListDTO) || !($VOClass instanceof SkitCategoryListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = SkitCategoryCtl::tag()->getSkitCategoryList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /skit/category/add
     * @auth skitCategoryAdd
     * @req Srv.Apps.Proto.DTO.Skit.SkitCategoryAddDTO
     * 短剧类别新增
     */
    public function skitCategoryAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitCategoryAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData    = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl     = LogCtl::tag();
        $logData    = $setData;
        $skcyId     = SkitCategoryCtl::tag()->skitCategoryAdd($setData);
        if($skcyId < 1){
            $errMsg = '短剧类别新增失败';
            $LogCtl->logAdd(LogCtl::SKIT_CATEGORY_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg     = '短剧类别新增成功';
        $LogCtl->logAdd(LogCtl::SKIT_CATEGORY_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /skit/category/edit
     * @auth skitCategoryEdit
     * @req Srv.Apps.Proto.DTO.Skit.SkitCategoryEditDTO
     * 短剧类别编辑
     */
    public function skitCategoryEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitCategoryEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $skcyId             = intval($DTOClass->getSkcyId());
        $setData            = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl             = LogCtl::tag();
        $SkitCategoryCtl    = SkitCategoryCtl::tag();
        $categorySrc        = $SkitCategoryCtl->getSkitCategorySrc($skcyId);
        if(!isset($categorySrc['skcy_id'])){
            $logData        = $setData;
            $errMsg         = '短剧类别不存在';
            $LogCtl->logAdd(LogCtl::SKIT_CATEGORY_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result             = $SkitCategoryCtl->skitCategoryEdit($skcyId, $setData);
        $logData            = ['new' => $setData, 'old' => $categorySrc];
        if(!$result){
            $errMsg         = '短剧类别编辑失败';
            $LogCtl->logAdd(LogCtl::SKIT_CATEGORY_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg             = '短剧类别编辑成功';
        $LogCtl->logAdd(LogCtl::SKIT_CATEGORY_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}